package xyz.ddlnt.user.usr.controller;

import cn.dev33.satoken.stp.StpUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import xyz.ddlnt.commonutil.constant.HttpConstants;
import xyz.ddlnt.commonutil.result.Result;
import xyz.ddlnt.commonutil.util.HttpUtils;
import xyz.ddlnt.model.dto.LoginDTO;
import xyz.ddlnt.model.dto.UserInfoDTO;
import xyz.ddlnt.model.entity.UserInfo;
import xyz.ddlnt.model.vo.BloggerInfoVO;
import xyz.ddlnt.model.vo.UserAchieveVO;
import xyz.ddlnt.serviceusrclient.client.UserFeignClient;
/**
 * @author ZZULI_SE 210910
 * @data 2025/5/6 22:36
 */
@Slf4j
@Tag(name = "用户接口测试")
@RequestMapping("/user")
@RestController
public class UserController {

    @Resource
    private UserFeignClient userFeignClient;
    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result<String > login(@RequestBody LoginDTO loginDTO) {
        return userFeignClient.login(loginDTO);
    }

    @Operation(summary = "用户登出")
    @GetMapping("/logout")
    public Result<Boolean> logout(HttpServletRequest request) {
        String satoken = request.getHeader(HttpConstants.SATOKEN);
        StpUtil.logout(StpUtil.getLoginIdByToken(satoken));
        return Result.success();
    }

    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody LoginDTO loginDTO) {
        return userFeignClient.register(loginDTO);
    }

    @Operation(summary = "用户注销")
    @DeleteMapping("/cancel")
    public Result<Boolean> cancel(@RequestBody LoginDTO loginDTO, HttpServletRequest request) {
        loginDTO.setId(HttpUtils.getId(request));
        return userFeignClient.cancel(loginDTO);
    }

    @Operation(summary = "获取用户信息")
    @GetMapping("/getUserInfo")
    public Result<UserInfo> getUserInfo(HttpServletRequest request) {
        Long id = HttpUtils.getId(request);
        return userFeignClient.getUserInfo(id);
    }

    @Operation(summary = "更新用户信息")
    @PostMapping("/updateUserInfo")
    public Result<Boolean> updateUserInfoById(@RequestBody UserInfoDTO userInfoDTO, HttpServletRequest request) {
        userInfoDTO.setId(HttpUtils.getId(request));
        return userFeignClient.updateUserInfoById(userInfoDTO);
    }

    @Operation(summary = "获取博主信息")
    @GetMapping("/getBloggerInfo/{bloggerId}")
    public Result<UserInfo> getBloggerInfo(@PathVariable Long bloggerId) {
        return userFeignClient.getBloggerInfo(bloggerId);
    }

    /**
     * 获取文章的博主信息
     * @param bloggerId
     * @return
     */
    @Operation(summary = "获取文章的博主信息")
    @GetMapping("/getPostBloggerInfo/{bloggerId}")
    public Result<BloggerInfoVO> getPostBloggerInfo(@PathVariable Long bloggerId) {
        return userFeignClient.getPostBloggerInfo(bloggerId);
    }

    @Operation(summary = "判断是否为用户本人")
    @GetMapping("/isMine/{userId}")
    public Result<Boolean> getUserInfo(@PathVariable Long userId, HttpServletRequest request) {
        return Result.success(HttpUtils.getId(request).equals(userId));
    }

    @Operation(summary = "获取用户成就信息")
    @GetMapping("/getUserAchieve")
    Result<UserAchieveVO> getUserAchieve(HttpServletRequest request) {
        return userFeignClient.getUserAchieve(HttpUtils.getId(request));
    }
}
